package com.reason.chat.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.reason.chat.Util.SensitivewordFilter;
import com.reason.chat.pojo.Afriend;
import com.reason.chat.pojo.SendMine;
import com.reason.chat.pojo.To;
import com.reason.chat.service.WebSocektService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 熊钒汐
 * @date 2020/6/30 13:46
 */
@Service
public class WebSocketServiceimpl implements WebSocektService {
    /**发送后接收端接收信息*/
    @Override
    public Map Socketmine(String message) {
        SensitivewordFilter filter=new SensitivewordFilter();
        JSONObject jsonObject = JSON.parseObject(message);
        SendMine mine = jsonObject.getJSONObject("data").getObject("mine", SendMine.class);
        To to = jsonObject.getJSONObject("data").getObject("to", To.class);
        Long time = jsonObject.getObject("time", Long.class);
        Map Sendmine = new HashMap();
        Sendmine.put("username", mine.getUsername());
        Sendmine.put("avatar", mine.getAvatar());
        Sendmine.put("id", mine.getId());
        Sendmine.put("content", filter.replaceSensitiveWord(mine.getContent(),2,"*"));
        Sendmine.put("type", to.getType());
        Sendmine.put("fromid", mine.getId());
        Sendmine.put("timestamp", time);
        Sendmine.put("mine", false);
        return Sendmine;
    }

    /**接收端信息*/
    @Override
    public To Socketto(String message) {
        JSONObject jsonObject = JSON.parseObject(message);
        To to = jsonObject.getJSONObject("data").getObject("to", To.class);
        return to;
    }

    @Override
    public Map Socketerror(String message) {
        To socketto = this.Socketto(message);
        Map map=new HashMap();
        map.put("system",true);
        map.put("id",socketto.getId());
        map.put("type","friend");
        map.put("content","该用户不在线上！");
        return map;
    }

    @Override
    public Map Socketstatus(Map map) {
        Map smap=new HashMap();
        smap.put("emit","status");
        smap.put("id",map.get("id"));
        smap.put("data",map.get("data"));
        return smap;
    }

    @Override
    public Map Socketadd(Afriend afriend) {
        Map map=new HashMap();
        map.put("emit","addlist");
        map.put("data",afriend);
        return map;
    }
}
